//
//  FLInterface.h
//  Pods
//
//  Created by LinXiaoBin on 16/3/15.
//
//

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, FLInterfaceErrorCode) {
    FLInterfaceErrorCodeURLError = -1000, //请求地址异常
    FLInterfaceErrorCodeParamError = -1001, //请求参数异常
};

@class FLInterfaceBase;
@protocol FLInterfaceResponseDelegate;
@protocol FLInterfaceManagerDelegate;
@protocol FLResponseSerializerDelegate;

typedef void(^FLInterfaceSucceedBlock_t)(FLInterfaceBase *interface, NSDictionary* responseHeader, id responseObject);
typedef void(^FLInterfaceFailedBlock_t)(FLInterfaceBase *interface, NSDictionary* responseHeader, NSError *error);

@interface FLInterfaceBase : NSObject

@property (nonatomic, copy) NSString *proxyIPString NS_AVAILABLE_IOS(8_0);
@property (nonatomic, assign) NSInteger maxTryReloadTimes; //默认重试2次

@property (nonatomic, weak) id<FLInterfaceResponseDelegate> delegate; //delegate 优先级高于block回调
@property (nonatomic, copy) FLInterfaceSucceedBlock_t succeedBlock;
@property (nonatomic, copy) FLInterfaceFailedBlock_t failedBlock;

@property (nonatomic, weak) id<FLInterfaceManagerDelegate> managerDelegate;
@property (nonatomic, strong) id<FLResponseSerializerDelegate> responseSerializer;

@property (nonatomic, strong) NSURL *url;
@property (nonatomic, copy) NSString *urlString;

- (instancetype)initWithDelegate:(id<FLInterfaceResponseDelegate>)delegate;
- (instancetype)initWithSucceedBlock:(FLInterfaceSucceedBlock_t)succeedBlock failedBlock:(FLInterfaceFailedBlock_t)failedBlock;

- (void)postURL:(NSString *)url header:(NSDictionary *)header dictionaryBody:(NSDictionary *)dictionary;
- (void)postURL:(NSString *)url header:(NSDictionary *)header stringBody:(NSString *)string encoding:(NSStringEncoding)encoding;
- (void)postURL:(NSString *)url header:(NSDictionary *)header dataBody:(NSData *)data;

- (void)getURL:(NSString *)urlString header:(NSDictionary *)header;

- (BOOL)tryReload;

- (void)failedRequest:(NSString *)url code:(NSInteger)code message:(NSString *)message;
- (void)failedRequest:(NSError *)error;
- (NSError *)errorWithDomain:(NSString *)domain code:(NSInteger)code message:(NSString *)message;

- (void)cancelAndClearDelegate;

#pragma mark DEPRECATED
- (void)postHttpsURL:(NSString *)urlString header:(NSDictionary<NSString *, NSString *> *)header dataBody:(NSData *)data __deprecated_msg("Use 'postURL:haeder:dataBody:'");

@end


@protocol FLResponseSerializerDelegate <NSObject>
@required
- (id)flInterface:(FLInterfaceBase *)interface serializerResponseHeader:(NSDictionary *)header responseObject:(id)responseObject error:(NSError **)error;
@end


@protocol FLInterfaceResponseDelegate <NSObject>
- (void)flInterface:(FLInterfaceBase *)interface didRequestFinished:(NSDictionary *)responseHeader responseObject:(id)responseObject;
- (void)flInterface:(FLInterfaceBase *)interface didRequestFailed:(NSDictionary *)responseHeader error:(NSError *)error;
@end


@protocol FLInterfaceManagerDelegate <NSObject>
@required
- (void)flInterfaceDidRequestFinished:(FLInterfaceBase *)interface;
@end

